package com.nchu.romantic.surprise.setting;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 * @author fujianjian
 * @project react-learning
 * @date 2019/8/5 22:26
 */
public class ServiceBeanDefinitionParser implements BeanDefinitionParser {

    private final Class beanClass;

    public ServiceBeanDefinitionParser(Class beanClass) {
        this.beanClass = beanClass;

    }

    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {

        RootBeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setBeanClass(beanClass);
        beanDefinition.setLazyInit(false);
        beanDefinition.getPropertyValues().add("id", element.getAttribute("id"));
        beanDefinition.getPropertyValues().add("name", element.getAttribute("name"));
        beanDefinition.getPropertyValues().add("path", element.getAttribute("path"));
        beanDefinition.getPropertyValues().add("clazz", element.getAttribute("class"));
        parserContext.getRegistry().registerBeanDefinition(beanClass.getName(), beanDefinition);
        return beanDefinition;
    }
}
